/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.library.internal.multichoice;

import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.view.base.CardViewWrapper;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */


public class MultiChoiceAdapterHelperBase implements ListContainer.ItemLongClickedListener, ListContainer.ItemClickedListener {

    protected static final String TAG = MultiChoiceAdapterHelperBase.class.getSimpleName();

    protected ListContainer mAdapterView;
    protected BaseItemProvider owner;
    //todo no such class
//    protected AbsListView.MultiChoiceModeListener mMultiChoiceModeListener;

    /**
     * ActionMode
     */
    //todo no such class
//    protected ActionMode actionMode;

    private boolean ignoreCheckedListener;

    // -------------------------------------------------------------
    // Constructors
    // -------------------------------------------------------------

    public MultiChoiceAdapterHelperBase(BaseItemProvider owner) {
        this.owner = owner;
    }

    // -------------------------------------------------------------
    // View
    // -------------------------------------------------------------

    /**
     * Sets the adapter and
     *
     * @param adapterView
     */
    public void setAdapterView(ListContainer adapterView) {
        mAdapterView = adapterView;
        //mAdapterView.setOnItemLongClickListener(this);
//        mAdapterView.setMultiChoiceModeListener(mMultiChoiceModeListener);
    }

    /**
     * Used to setup some element events for multichoice
     *
     * @param view
     * @param mCard
     * @param mCardView
     * @param position
     */
    public void setupMultichoice(Component view, Card mCard, CardViewWrapper mCardView, long position) {
        final MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;


        Component.ClickedListener advanceClickListener = new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                final CardViewWrapper cardView = (CardViewWrapper) v;
                int position = adapter.getPosition(cardView.getCard());
                onItemClicked(mAdapterView, v, position, adapter.getItemId(position));
            }
        };


        //You need it to enable the CAB
        if (mCard.isCheckable()) {
            //mCardView.setLongClickable(true);

            mCardView.setClickedListener(advanceClickListener);
        } else {
            if (mCard.getOnClickListener() != null) {
                mCardView.setClickedListener(advanceClickListener);
            }
        }
    }

    /**
     * Checks and unchecks the items
     *
     * @param handle  position
     * @param checked true if item is checked, false otherwise
     */
    protected void setItemChecked(long handle, boolean checked) {
        if (checked) {
            checkItem(handle);
        } else {
            uncheckItem(handle);
        }
    }

    /**
     * Checks the item
     *
     * @param handle
     */
    protected void checkItem(long handle) {
        //todo no such method
//        mAdapterView.setItemChecked((int) handle, true);
    }

    /**
     * Unckecks the item
     *
     * @param handle
     */
    protected void uncheckItem(long handle) {
        //todo no such method
//        mAdapterView.setItemChecked((int) handle, false);
    }

    // -------------------------------------------------------------
    // OnItemLongClickListener implementation
    // -------------------------------------------------------------
    @Override
    public boolean onItemLongClicked(ListContainer adapterView, Component component, int position, long id) {

        //Check if the card is checkable
        if (!isCardCheckable(position)) {
            return false;
        }


        //Check the item
        int correctedPosition = correctPositionAccountingForHeader(adapterView, position);
        long handle = positionToSelectionHandle(correctedPosition);
        //todo no such method
//        boolean wasChecked = mAdapterView.isItemChecked((int) handle);
//
//        // invoke the contextual action mode by setting the respective list item to the checked
//        setItemChecked(handle, !wasChecked);
//        component.setActivated(!wasChecked);

//        if (actionMode != null) {
//            //You need it to enable the CAB
//            //((CardView)view).setLongClickable(false);
//        }

        return true;

    }

    private int correctPositionAccountingForHeader(ListContainer listView, int position) {
        //todo no such method
//        int headersCount = listView == null ? 0 : listView.getHeaderViewsCount();
//        if (headersCount > 0) {
//            position -= listView.getHeaderViewsCount();
//        }
        return 0;
    }

    protected long positionToSelectionHandle(int position) {
        return position;
    }

    /**
     * Indicates if the card is checkable
     *
     * @param position
     * @return boolean
     */
    public boolean isCardCheckable(int position) {
        MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
        Card card = adapter.getItem(position);
        if (card != null)
            return card.isCheckable();

        return false;
    }

    // -------------------------------------------------------------
    // OnItemClickListener implementation
    // -------------------------------------------------------------
    @Override
    public void onItemClicked(ListContainer parent, Component component, int position, long id) {
        MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
        if (adapter.getOptionMultiChoice().isSelectItemClickInActionMode()) {
            if (adapter.isActionModeStarted()) {
                onItemLongClicked(parent, component, position, id);
                return;
            } else {
                //Default card onItemClick
                internal_onItemClick(parent, component, position, id);
            }
        } else {
            //Default card onItemClick
            internal_onItemClick(parent, component, position, id);
        }
    }


    public void internal_onItemClick(ListContainer parent, Component view, int position, long id) {
        MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
        Card mCard = adapter.getItem(position);
        if (mCard != null && mCard.getOnClickListener() != null)
            mCard.getOnClickListener().onClick(mCard, view);
    }

    // -------------------------------------------------------------
    // ActionMode
    // -------------------------------------------------------------
    //todo no such method
//    public boolean startActionMode(Ability activity) {
//        if (activity != null) {
//            if (!isActionModeStarted())
//                activity.startActionMode(mMultiChoiceModeListener);
//        }
//        return false;
//    }

    /**
     * Called when action mode is first created. The menu supplied will be used to
     * generate action buttons for the action mode.
     *
     * @param mode ActionMode being created
     * @param menu Menu used to populate action buttons
     * @return true if the action mode should be created, false if entering this
     * mode should be aborted.
     */
    //todo no such method
//    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
////        actionMode = mode;
////        onItemSelectedStateChanged(mode);
////        //mAdapterView.setOnItemLongClickListener(null);
////        //mAdapterView.setLongClickable(false);
////        return false;
////    }

    /**
     * Called when an action mode is about to be exited and destroyed.
     *
     * @param mode The current ActionMode being destroyed
     */
    //todo no such method
//    public void onDestroyActionMode(ActionMode mode) {
//        actionMode = null;
//        //mAdapterView.setOnItemLongClickListener(this);
//    }

    //todo no such method
    /**
     * Called when an item is checked or unchecked during selection mode.
     *
     * @param mode     The {@link ActionMode} providing the selection mode
     * @param position Adapter position of the item that was checked or unchecked
     * @param id       Adapter ID of the item that was checked or unchecked
     * @param checked  <code>true</code> if the item is now checked, <code>false</code>
     *                 if the item is now unchecked.
     */
//    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
//        onItemSelectedStateChanged(mode);
//        MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
//        Card card = adapter.getItem(position);
//        adapter.onItemCheckedStateChanged(mode, position, id, checked, card.getCardView(), card);
//    }

    /**
     * Manage the title when the items are selected
     *
     * @param mode
     */
    //todo no such method
//    protected void onItemSelectedStateChanged(ActionMode mode) {
//        int count = mAdapterView.getCheckedItemCount();
//
//        if (count > 0) {
//            Resources res = mAdapterView.getResources();
//            String mTitleSelected = res.getQuantityString(R.plurals.card_selected_items, count, count);
//            mode.setTitle(mTitleSelected);
//        }
//    }

    /**
     * Indicate if action mode is started
     *
     * @return
     */
    //todo no such method
//    public boolean isActionModeStarted() {
//        if (actionMode != null) {
//            return true;
//        }
//        return false;
//    }


    // -------------------------------------------------------------
    // Utility methods
    // -------------------------------------------------------------

    /**
     * Returns the selected cards
     *
     * @return
     */
    //todo no such method
//    public ArrayList<Card> getSelectedCards() {
//        SparseBooleanArray checked = mAdapterView.getCheckedItemPositions();
//        ArrayList<Card> items = new ArrayList<Card>();
//        MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
//
//
//        for (int i = checked.size() - 1; i >= 0; i--) {
//            if (checked.valueAt(i) == true) {
//                items.add(adapter.getItem((int) checked.keyAt(i)));
//            }
//        }
//
//        return items;
//    }
    // -------------------------------------------------------------
    // Getters and setters
    // -------------------------------------------------------------
    //todo no such method
//    /**
//     * Get the {@link openHarmony.widget.AbsListView.MultiChoiceModeListener}
//     *
//     * @return
//     */
//    public AbsListView.MultiChoiceModeListener getMultiChoiceModeListener() {
//        return mMultiChoiceModeListener;
//    }
//
//    /**
//     * Set the {@link openHarmony.widget.AbsListView.MultiChoiceModeListener}
//     *
//     * @param multiChoiceModeListener
//     */
//    public void setMultiChoiceModeListener(AbsListView.MultiChoiceModeListener multiChoiceModeListener) {
//        mMultiChoiceModeListener = multiChoiceModeListener;
//    }


}